package tz.postprocessing;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.StringTokenizer;
import java.util.TreeMap;

public class DataParser {
	
	public  static TreeMap<String, HashSet<Integer>> clustersTopics = new TreeMap<String, HashSet<Integer>>();
	public  static TreeMap<String, HashSet<Integer>> clustersTopicsResults = new TreeMap<String, HashSet<Integer>>();
	public  TreeMap<String, HashSet<Integer>> clustersTopicsOne = new TreeMap<String, HashSet<Integer>>();

	public DataParser(){
		
	}
	
	public static TreeMap<String, HashSet<Integer>> getclusterTopics(){
		return clustersTopics;
	}
	
	public static TreeMap<String, HashSet<Integer>> getclusterTopicsResults(){
		return clustersTopicsResults;
	}
	
	
	
	public static void readFromFiels(String filenameResults, String filenameTest){
		
		try{
		    //Najprej sparsamo testne podatke
		    FileInputStream fstream = new FileInputStream(filenameTest);
		    // Get the object of DataInputStream
		    DataInputStream in = new DataInputStream(fstream);
		        BufferedReader br = new BufferedReader(new InputStreamReader(in));
		    String strLine;
		    //Read File Line By Line
		    while ((strLine = br.readLine()) != null)   {
		    	String[] tokens = strLine.split("\t");
		    	String Artid = tokens[0];
		    	if(tokens[0].contains(".")){ Artid = tokens[0].substring(0, tokens[0].length()-2);}
		    	Integer articleId = Integer.parseInt(Artid);
		    	String cluster = tokens[1];
		    	putIntoclusterTopics(cluster, articleId);


		    	
		    }
		    //Close the input stream
		    in.close();
		    
		    //potem pa se rezultate
		    fstream = new FileInputStream(filenameResults);
		    // Get the object of DataInputStream
		    in = new DataInputStream(fstream);
		        br = new BufferedReader(new InputStreamReader(in));
		    
		    //Read File Line By Line
		    while ((strLine = br.readLine()) != null)   {
		    	String[] tokens = strLine.split("\t");
		    	if(tokens[0]== "") continue;
		    	Integer articleId = Integer.parseInt(tokens[0]);
		    	String cluster = tokens[1];
	    		putIntoclusterTopicsResult(cluster, articleId);

		    	
		    }
		    //Close the input stream
		    in.close();
		    
		    System.out.println("IM DONE");
		    
		    
		    
		    }catch (Exception e){//Catch exception if any
		      System.err.println("Error: " + e.getMessage());
		      e.printStackTrace();
		}
		    	
	}
	
	
	public  TreeMap<String, HashSet<Integer>> readFromFile(String filename){
		
		try{
		    //Najprej sparsamo testne podatke
		    FileInputStream fstream = new FileInputStream(filename);
		    // Get the object of DataInputStream
		    DataInputStream in = new DataInputStream(fstream);
		        BufferedReader br = new BufferedReader(new InputStreamReader(in));
		    String strLine;
		    //Read File Line By Line
		    while ((strLine = br.readLine()) != null)   {
		    	String[] tokens = strLine.split("\t");
		    	String Artid = tokens[0];
		    	if(tokens[0].contains(".")){ Artid = tokens[0].substring(0, tokens[0].length()-2);}
		    	Integer articleId = Integer.parseInt(Artid);
		    	String cluster = tokens[1];
		    	putIntoclusterTopicsOne(cluster, articleId);


		    	
		    }
		    //Close the input stream
		    in.close();
		}catch (Exception e){//Catch exception if any
		      System.err.println("Error: " + e.getMessage());
		      e.printStackTrace();
		}
		return this.clustersTopicsOne;
		    
	}
	
	private static void putIntoclusterTopics(String cluster, Integer articleId){
		HashSet<Integer> ArticleIdSet = clustersTopics.get(cluster); 
    	if( ArticleIdSet != null){
    		ArticleIdSet.add(articleId);
    		clustersTopics.put(cluster, ArticleIdSet);
    	}else{
    		ArticleIdSet = new HashSet<Integer>();
    		ArticleIdSet.add(articleId);
    		clustersTopics.put(cluster, ArticleIdSet);
    	}	
	}
	
	private  void putIntoclusterTopicsOne(String cluster, Integer articleId){
		HashSet<Integer> ArticleIdSet = clustersTopicsOne.get(cluster); 
    	if( ArticleIdSet != null){
    		ArticleIdSet.add(articleId);
    		clustersTopicsOne.put(cluster, ArticleIdSet);
    	}else{
    		ArticleIdSet = new HashSet<Integer>();
    		ArticleIdSet.add(articleId);
    		clustersTopicsOne.put(cluster, ArticleIdSet);
    	}	
	}
	
	private static void putIntoclusterTopicsResult(String cluster, Integer articleId){
		HashSet<Integer>  ArticleIdSet = clustersTopicsResults.get(cluster); 
    	if( ArticleIdSet != null){
    		ArticleIdSet.add(articleId);
    		clustersTopicsResults.put(cluster, ArticleIdSet);
    	}else{
    		ArticleIdSet = new HashSet<Integer>();
    		ArticleIdSet.add(articleId);
    		clustersTopicsResults.put(cluster, ArticleIdSet);
    	}	
	}

	public static void wrriteClustersToFile(String filename, TreeMap<String, HashSet<Integer>> clustersTopics){
		  try{
			    // Create file 
			    FileWriter fstream = new FileWriter(filename);
			        BufferedWriter out = new BufferedWriter(fstream);
			    
			    for(String topics : clustersTopics.keySet()){
			    	HashSet<Integer> IdArticle = clustersTopics.get(topics);
			    	Iterator<Integer> iter = IdArticle.iterator();
			    	while(iter.hasNext()){
			    		Integer id = iter.next();
			    		String str = id.toString()+"\t"+topics+"\n";
			    		out.write(str);
			    	}
			    }
			    
			    //Close the output stream
			    out.close();
			    }catch (Exception e){//Catch exception if any
			      System.err.println("Error: " + e.getMessage());
			      e.printStackTrace();
			    }
	}

}
